import java.util.Scanner;

class Main4{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in) ;
        int n = scan.nextInt() ;
        String a = scan.next() ;
        String b = scan.next() ;
        if (n < 2) {
            System.out.println(-1) ;
            return ;
        }
        int len = -1 ;
        String prev = null ;
        int previ = 0 ;
        for (int i = 0 ; i < n ; i ++) {
            String str = scan.next() ;
            if (a.equals(str) && b.equals(prev)) {
                if (len < 0) {
                    len = i - previ ;
                } else {
                    len = Math.min(len, i - previ) ;
                }
                prev = a ;
                previ = i ;
            } else if (b.equals(str) && a.equals(prev)) {
                if (len < 0) {
                    len = i - previ ;
                } else {
                    len = Math.min(len, i - previ) ;
                }
                prev = b ;
                previ = i ;

            }
        }

        System.out.println(len) ;

    }
}